package org.robot.work;

import java.util.List;
import java.util.stream.Stream;

import org.jsoup.helper.StringUtil;
import org.robot.Spider;
import org.robot.component.Page;
import org.robot.dao.AuthorDao;
import org.robot.dao.IssueDao;
import org.robot.dao.JournalDao;
import org.robot.dao.PaperDao;
import org.robot.pojo.Author;
import org.robot.pojo.Issue;
import org.robot.pojo.Journal;
import org.robot.pojo.Paper;
import org.robot.utils.MatchEmail;
import org.robot.utils.RegexUtil;

/**
 * 已入库
 * @author pdz
 * @date 2019年1月10日
 */
public class Aspet extends Spider {
	private static final Long TASKID = 1000L;

	@Override
	public void parse(Page page) throws Exception {
		if (page.typeEquals("journal")) {
//			parseJournal(page);
		}
		else if (page.typeEquals("year")) {
			page.setNextLinksFilter(e -> e.text().contains(":"));
		} else if (page.typeEquals("issue")) {
			page.setNextLinksFilter(e -> !StringUtil.isBlank(e.text()));
//			parseIssue(page);
		} else if (page.typeEquals("paper")) {
//			parsePaper(page);
		}
		else if (page.typeEquals("paperinfo")) {
			parsePaperInfo1(page);
		}
		
	}
	

	private void parsePaperInfo1(Page page) {
		Long paperid = PaperDao.selectPidByWebsite(Paper.class, page.prevUrl(), TASKID);
		List<String> list = page.select("meta[name=DC.Contributor]").eachAttr("content");
		String[] authors = list.toArray(new String[list.size()]);
		String email = page.select(".em-addr").text().replace("{at}", "@").trim();
		if (email.contains(" ")) {
			String[] emailArr = email.split(" +");
			for (String em : emailArr) {
				String matchAuthor = MatchEmail.similarName(authors, em);
				Author author = new Author();
				author.setFullName(matchAuthor);
				author.setTaskId(TASKID);
				author.setPaperid(paperid);
				author.setEmail(em);
				boolean b = AuthorDao.addAuthor(author);
				System.out.println(b ? "add email success" : "add email fair");
			}
		}
	}
	
	private void parsePaperInfo(Page page) {
		List<String> list = page.select("meta[name=DC.Contributor]").eachAttr("content");
		String[] authors = list.toArray(new String[list.size()]);
		String email = page.select(".em-addr").text().replace("{at}", "@").trim();
		String matchAuthor = MatchEmail.similarName(authors, email);
		Long paperid = PaperDao.selectPidByWebsite(Paper.class, page.prevUrl(), TASKID);
		for (String aut : authors) {
			Author author = new Author();
			author.setFullName(aut);
			author.setTaskId(TASKID);
			author.setPaperid(paperid);
			if (aut.equals(matchAuthor))
				author.setEmail(email);
			AuthorDao.addAuthor(author);
		}
	}


	private void parsePaper(Page page) {
		String doi = page.select("meta[name=DC.Identifier]").attr("content");
		String abst = page.select("meta[name=DC.Description]").attr("content");
		Long issueid = IssueDao.selectPidByWebsite(Issue.class, page.prevUrl(), TASKID);
		
		Paper paper = new Paper();
		paper.setWebsite(page.url());
		paper.setTitle(page.urlText());
		paper.setTaskid(TASKID);
		paper.setDoi(doi);
		paper.setAbstracts(abst);
		paper.setPdfUrl(page.url() + ".full.pdf");
		paper.setIssueId(issueid);
		PaperDao.addPaper(paper);
	}


	private void parseJournal(Page page) {
		String str = page.select(".snippet-content").text();
		String issn = RegexUtil.getIssn(str);
		String name = page.select("title").text().split("\\|")[1].trim();
		System.out.println(page.url() + "---" + name);
		Journal journal = new Journal();
		journal.setWebsite(page.url());
		journal.setIssn(issn);
		journal.setTaskId(TASKID);
		journal.setJournalname(name);
		JournalDao.addJournal(journal);
	}


	private void parseIssue(Page page) {
		String str = page.urlText();
		String issueVal = str.substring(0, str.lastIndexOf(":")).trim();
		String year = RegexUtil.regexFirst("2\\d{3}", issueVal);
		Long journalid = JournalDao.selectPidByWebsite(Journal.class, page.prevUrl(3), TASKID);
		
		Issue issue = new Issue();
		issue.setIssue(issueVal);
		issue.setYear(year);
		issue.setTaskId(TASKID);
		issue.setWebsite(page.url());
		issue.setJournalId(journalid);
		IssueDao.addIssue(issue);
	}


	public static void main(String[] args) {
		Aspet claw = new Aspet();
		claw.setBaseDirectory("E:/webSpider/Aspet");
//		claw.getConfig().setInterval(10);
		claw.addSeed("https://www.aspet.org");
		claw.addRegex("http://[a-z]+.aspetjournals.org", "journal");
		claw.addRegex("http://[a-z]+.aspetjournals.org/content/by/year", "archive");
		claw.addRegex("http://[a-z]+.aspetjournals.org/content/by/year/2\\d{3}", "year");
		claw.addRule("a.hw-issue-meta-data", "http://[a-z]+.aspetjournals.org/content/\\d+/\\d+", "issue");
		claw.addRule(".toc-section .toc-item a", "http://[a-z]+.aspetjournals.org/content/\\d+/\\d+/\\d+", "paper");
		claw.addRegex("http://[a-z]+.aspetjournals.org/content/\\d+/\\d+/\\d+/tab-article-info", "paperinfo");
		claw.start();
		
	}

}
